﻿@Cofe ?= {}

uriUtils =  
  included : ->
    #console.log("imported")

  formatPageStr : (itemsPerPage, page) ->
    if page is undefined then page = 1
    return "" + page + "," + itemsPerPage
  
  updateParam : (params, key, value) ->
    if typeof params is "number"
      throw "Wrong arguments"
    if params is null or params is undefined then params = {}
    params[key] = value
    return params

  apiEntryPath : "/api/entry"
  apiParsePath : "/api/parse"
  apiSearchPath : "/api/search"

  formatId : (id, itemsPerPage, page) ->
    if id is undefined
      throw "id not defined"
    return @formatUri(@apiEntryPath + "/" + id, undefined, itemsPerPage, page)

  formatUri : (uri, params, itemsPerPage, page) ->    
    if itemsPerPage isnt undefined 
      pageStr = @formatPageStr(itemsPerPage, page)
      params = @updateParam(params, "page", pageStr)
    if params isnt null then uri + "?" + $.param(params, true) else uri
  
  formatPath : (path, params, itemsPerPage, page) ->
    params = @updateParam(params, "path", path)        
    return @formatUri(@apiParsePath, params, itemsPerPage, page)

  formatSearch : (filterStr, params, itemsPerPage, page) ->
    params = @updateParam(params, "filterStr", filterStr)  
    return @formatUri(@apiSearchPath, params, itemsPerPage, page)
    
Cofe.uriUtils = uriUtils